<!DOCTYPE html>
<html>
<head>
<title>total.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

body {
	font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
	font-size: var(--vscode-markdown-font-size, 14px);
	padding: 0 26px;
	line-height: var(--vscode-markdown-line-height, 22px);
	word-wrap: break-word;
}

#code-csp-warning {
	position: fixed;
	top: 0;
	right: 0;
	color: white;
	margin: 16px;
	text-align: center;
	font-size: 12px;
	font-family: sans-serif;
	background-color:#444444;
	cursor: pointer;
	padding: 6px;
	box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}

#code-csp-warning:hover {
	text-decoration: none;
	background-color:#007acc;
	box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}

body.scrollBeyondLastLine {
	margin-bottom: calc(100vh - 22px);
}

body.showEditorSelection .code-line {
	position: relative;
}

body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
	content: "";
	display: block;
	position: absolute;
	top: 0;
	left: -12px;
	height: 100%;
}

body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
	left: -30px;
}

.vscode-light.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(0, 0, 0, 0.15);
}

.vscode-light.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(0, 0, 0, 0.40);
}

.vscode-light.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-dark.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 255, 255, 0.4);
}

.vscode-dark.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 255, 255, 0.60);
}

.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-high-contrast.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 160, 0, 0.7);
}

.vscode-high-contrast.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 160, 0, 1);
}

.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

img {
	max-width: 100%;
	max-height: 100%;
}

a {
	text-decoration: none;
}

a:hover {
	text-decoration: underline;
}

a:focus,
input:focus,
select:focus,
textarea:focus {
	outline: 1px solid -webkit-focus-ring-color;
	outline-offset: -1px;
}

hr {
	border: 0;
	height: 2px;
	border-bottom: 2px solid;
}

h1 {
	padding-bottom: 0.3em;
	line-height: 1.2;
	border-bottom-width: 1px;
	border-bottom-style: solid;
}

h1, h2, h3 {
	font-weight: normal;
}

table {
	border-collapse: collapse;
}

table > thead > tr > th {
	text-align: left;
	border-bottom: 1px solid;
}

table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
	padding: 5px 10px;
}

table > tbody > tr + tr > td {
	border-top: 1px solid;
}

blockquote {
	margin: 0 7px 0 5px;
	padding: 0 16px 0 10px;
	border-left-width: 5px;
	border-left-style: solid;
}

code {
	font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
	font-size: 1em;
	line-height: 1.357em;
}

body.wordWrap pre {
	white-space: pre-wrap;
}

pre:not(.hljs),
pre.hljs code > div {
	padding: 16px;
	border-radius: 3px;
	overflow: auto;
}

pre code {
	color: var(--vscode-editor-foreground);
	tab-size: 4;
}

/** Theming */

.vscode-light pre {
	background-color: rgba(220, 220, 220, 0.4);
}

.vscode-dark pre {
	background-color: rgba(10, 10, 10, 0.4);
}

.vscode-high-contrast pre {
	background-color: rgb(0, 0, 0);
}

.vscode-high-contrast h1 {
	border-color: rgb(0, 0, 0);
}

.vscode-light table > thead > tr > th {
	border-color: rgba(0, 0, 0, 0.69);
}

.vscode-dark table > thead > tr > th {
	border-color: rgba(255, 255, 255, 0.69);
}

.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
	border-color: rgba(0, 0, 0, 0.18);
}

.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
	border-color: rgba(255, 255, 255, 0.18);
}

</style>

<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */

/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
	color: #8e908c;
}

/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
	color: #c82829;
}

/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
	color: #f5871f;
}

/* Tomorrow Yellow */
.hljs-attribute {
	color: #eab700;
}

/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
	color: #718c00;
}

/* Tomorrow Blue */
.hljs-title,
.hljs-section {
	color: #4271ae;
}

/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
	color: #8959a8;
}

.hljs {
	display: block;
	overflow-x: auto;
	color: #4d4d4c;
	padding: 0.5em;
}

.hljs-emphasis {
	font-style: italic;
}

.hljs-strong {
	font-weight: bold;
}
</style>

<style>
/*
 * Markdown PDF CSS
 */

 body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
	padding: 0 12px;
}

pre {
	background-color: #f8f8f8;
	border: 1px solid #cccccc;
	border-radius: 3px;
	overflow-x: auto;
	white-space: pre-wrap;
	overflow-wrap: break-word;
}

pre:not(.hljs) {
	padding: 23px;
	line-height: 19px;
}

blockquote {
	background: rgba(127, 127, 127, 0.1);
	border-color: rgba(0, 122, 204, 0.5);
}

.emoji {
	height: 1.4em;
}

code {
	font-size: 14px;
	line-height: 19px;
}

/* for inline code */
:not(pre):not(.hljs) > code {
	color: #C9AE75; /* Change the old color so it seems less like an error */
	font-size: inherit;
}

/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
	page-break-after: always;
}

</style>

<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
  <script>
    mermaid.initialize({
      startOnLoad: true,
      theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
          ? 'dark'
          : 'default'
    });
  </script>
<h1 id="%E5%AF%B9%E4%BF%A1%E5%8F%B7%E8%BF%9B%E8%A1%8C%E5%82%85%E9%87%8C%E5%8F%B6%E5%8F%98%E6%8D%A2">对信号进行傅里叶变换</h1>
<h2 id="%E7%AC%AC%E4%B8%80%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第一次作业）</h2>
<h2 id="%E4%B8%80-%E6%89%93%E5%BC%80%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E6%95%B0%E6%8D%AE%E5%B9%B6%E6%98%BE%E7%A4%BA">一、 打开心电信号数据并显示</h2>
<h3 id="1-%E7%A8%8B%E5%BA%8F">1. <strong>程序</strong></h3>
<pre class="hljs"><code><div>    fid1 = fopen(<span class="hljs-string">'G:\乱七八糟的文献\SmartHealth-master\SmartHealth-master\DATA\mitbd\100.dat'</span>);  
    ecg = fread(fid1,<span class="hljs-string">'short'</span>); <span class="hljs-comment">%以short型读取文件  </span>
    fclose(fid1);  

    fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
    gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>
    t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间  </span>
    <span class="hljs-built_in">figure</span>;  
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)/gain); <span class="hljs-comment">%画图显示前2500个点  </span>
    ylabel(<span class="hljs-string">'mV'</span>);  
    xlabel(<span class="hljs-string">'时间（秒）'</span>);  
</div></code></pre>
<h3 id="2-%E7%BB%93%E6%9E%9C%E5%9B%BE%E7%89%87">2. <strong>结果（图片）</strong></h3>
<p><img src="./../homework1/这才是真正的第一次的结果.png" alt=""></p>
<hr>
<h2 id="%E4%BA%8C%E5%AF%B9%E4%BF%A1%E5%8F%B7%E8%BF%9B%E8%A1%8C%E5%82%85%E9%87%8C%E5%8F%B6%E5%8F%98%E6%8D%A2%E5%B9%B6%E6%98%BE%E7%A4%BA%E9%A2%91%E5%9F%9F%E5%9B%BE%E5%BD%A2">二、对信号进行傅里叶变换并显示频域图形</h2>
<h3 id="1-%E7%A8%8B%E5%BA%8F">1. <strong>程序</strong></h3>
<pre class="hljs"><code><div>    <span class="hljs-comment">%初始条件  </span>
    fs=<span class="hljs-number">250</span>; <span class="hljs-comment">%设采样频率为250Hz  </span>
    f1=<span class="hljs-number">10</span>; <span class="hljs-comment">%设第一个信号频率为10Hz   </span>
    f2=<span class="hljs-number">12</span>; <span class="hljs-comment">%设第二个信号频率为12Hz  </span>
    T=<span class="hljs-number">10</span>; <span class="hljs-comment">%显示10s的信号  </span>

    <span class="hljs-comment">%计算  </span>
    t=<span class="hljs-number">0</span>:<span class="hljs-number">1</span>/fs:T<span class="hljs-number">-1</span>/fs; <span class="hljs-comment">%将采样间隔用时间表示，使横轴坐标为0到T  </span>
    x=<span class="hljs-built_in">cos</span>(<span class="hljs-number">2</span>*<span class="hljs-built_in">pi</span>*f1*t)+<span class="hljs-built_in">cos</span>(<span class="hljs-number">2</span>*<span class="hljs-built_in">pi</span>*f2*t)+<span class="hljs-built_in">randn</span>(<span class="hljs-number">1</span>,<span class="hljs-built_in">length</span>(t)); <span class="hljs-comment">%生成频率分别为f1、f2的余弦函数以及随机噪声组成的信号  </span>
    Fz=<span class="hljs-built_in">abs</span>(fft(x)); <span class="hljs-comment">%对生成的信号进行傅里叶变换  </span>
    f=<span class="hljs-number">0</span>:<span class="hljs-number">1</span>/<span class="hljs-built_in">length</span>(t)*fs:(<span class="hljs-number">1</span><span class="hljs-number">-1</span>/<span class="hljs-built_in">length</span>(t))*fs; <span class="hljs-comment">%将横轴坐标从时间转换为频率  </span>

    <span class="hljs-comment">%画图  </span>
    <span class="hljs-built_in">figure</span>;   
    subplot(<span class="hljs-number">211</span>);  
    <span class="hljs-built_in">plot</span>(t,x); <span class="hljs-comment">%画出原始信号的时域图形  </span>
    xlabel(<span class="hljs-string">'时间（秒）'</span>);  
    ylabel(<span class="hljs-string">'幅度'</span>);  
    subplot(<span class="hljs-number">212</span>);  
    <span class="hljs-built_in">plot</span>(f,Fz); <span class="hljs-comment">%画出傅里叶变换后的频域图形  </span>
    xlabel(<span class="hljs-string">'频率（Hz）'</span>);  
    ylabel(<span class="hljs-string">'幅度'</span>);  
</div></code></pre>
<h3 id="2-%E7%BB%93%E6%9E%9C%E5%9B%BE%E7%89%87">2. <strong>结果（图片）</strong></h3>
<p><img src="./../homework1/第一次结果.png" alt="">  <div STYLE="page-break-after: always;"></div></p>
<h1 id="%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E7%9A%84%E9%A2%84%E5%A4%84%E7%90%86">心电信号的预处理</h1>
<h2 id="%E7%AC%AC%E4%BA%8C%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第二次作业）</h2>
<h2 id="%E4%B8%80%E5%AF%B9%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E8%BF%9B%E8%A1%8C%E5%B9%B3%E6%BB%91%E6%BB%A4%E6%B3%A2">一、对心电信号进行平滑滤波</h2>
<h3 id="1%E7%A8%8B%E5%BA%8F">1.程序</h3>
<pre class="hljs"><code><div>    <span class="hljs-comment">%打开文件</span>
    fid1 = fopen(<span class="hljs-string">'G:\lqbzdwx\SmartHealth-master\SmartHealth-master\DATA\mitbd\100.dat'</span>);  
    ecg = fread(fid1,<span class="hljs-string">'short'</span>); <span class="hljs-comment">%以short型读取文件  </span>
    fclose(fid1);

    fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
    gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>
    t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间  </span>
    ecg = ecg/gain; <span class="hljs-comment">%去除信号自带增益？</span>

    <span class="hljs-comment">%平滑滤波器</span>
    ylabel(<span class="hljs-string">'幅度（mV）'</span>);
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    b = <span class="hljs-built_in">ones</span>(<span class="hljs-number">10</span>,<span class="hljs-number">1</span>)/<span class="hljs-number">20</span>;
    a = <span class="hljs-number">1</span>;
    t1 = filter(b,a,ecg);
    freqz(b,a);
    b = <span class="hljs-built_in">ones</span>(<span class="hljs-number">20</span>,<span class="hljs-number">1</span>)/<span class="hljs-number">20</span>;
    t2 = filter(b,a,ecg);
    <span class="hljs-built_in">figure</span>;
    freqz(b,a);
    <span class="hljs-built_in">figure</span>;
    <span class="hljs-built_in">hold</span> on;
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),t1(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),<span class="hljs-string">'-r'</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),t2(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),<span class="hljs-string">'-y'</span>);
    <span class="hljs-built_in">legend</span>(<span class="hljs-string">'ecg'</span>,<span class="hljs-string">'10阶平滑'</span>,<span class="hljs-string">'20阶平滑'</span>);
</div></code></pre>
<h3 id="2%E7%BB%93%E6%9E%9C%E5%9B%BE%E7%89%87">2.结果（图片）</h3>
<p><strong>平滑滤波前后对比</strong>
<img src="./../homework2/平滑滤波结果对比.png" alt="">
<strong>10阶平滑滤波频率响应特性图</strong>
<img src="./../homework2/10阶平滑滤波频率响应特性图.png" alt="">
<strong>20阶平滑滤波频率响应特性图</strong>
<img src="./../homework2/20阶平滑滤波频率响应特性图.png" alt=""></p>
<h2 id="%E4%BA%8C-%E5%AF%B9%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E8%BF%9B%E8%A1%8C%E6%BB%A4%E6%B3%A2%E5%92%8C%E9%A2%84%E5%A4%87%E8%AF%86%E5%88%ABqrs%E6%B3%A2">二、 对心电信号进行滤波和预备识别QRS波</h2>
<h3 id="1-%E7%A8%8B%E5%BA%8F">1. 程序</h3>
<pre class="hljs"><code><div>    <span class="hljs-comment">%打开文件</span>
    fid1 = fopen(<span class="hljs-string">'G:\lqbzdwx\SmartHealth-master\SmartHealth-master\DATA\mitbd\100.dat'</span>);  
    ecg = fread(fid1,<span class="hljs-string">'short'</span>); <span class="hljs-comment">%以short型读取文件  </span>
    fclose(fid1);

    fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
    gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>
    t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间  </span>
    ecg = ecg/gain; <span class="hljs-comment">%去除信号自带增益？</span>

    <span class="hljs-comment">% for i=1:size(b,2) (for回圈说：你不要抛弃我啊球球了！！我：...哎呀，那好吧....(10min后)对不起我始乱终弃了</span>

    <span class="hljs-comment">%低通滤波：y(n)=2y(n-1)-y(n-2)+x(n)-2x(n-6)+x(n-12)</span>
    b = [<span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">-2</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span>];
    a = [<span class="hljs-number">1</span> <span class="hljs-number">-2</span> <span class="hljs-number">1</span>];
    y1 = filter(b,a,ecg)/<span class="hljs-number">36</span>; <span class="hljs-comment">%低通滤波器增益是36倍</span>
    <span class="hljs-built_in">figure</span>;
    freqz(b,a);
    <span class="hljs-comment">%高通滤波：y(n)=y(n-1)+x(n)-x(n-32),z(n)=x(n-16)-y(n)/32 OR y(nT)=32x(nT-16T)-[y(nT-T)+x(nT)-x(nT-32T)]</span>
    b = [<span class="hljs-number">1</span> <span class="hljs-built_in">zeros</span>(<span class="hljs-number">1</span>,<span class="hljs-number">31</span>) <span class="hljs-number">-1</span>];
    a = [<span class="hljs-number">1</span>,<span class="hljs-number">-1</span>];
    y2 = filter(b,a,y1);
    b = [<span class="hljs-built_in">zeros</span>(<span class="hljs-number">1</span>,<span class="hljs-number">16</span>),<span class="hljs-number">1</span>];
    a = <span class="hljs-number">1</span>;
    y3 = filter(b,a,y1)-y2/<span class="hljs-number">32</span>; <span class="hljs-comment">%高通滤波器增益是32倍</span>
    <span class="hljs-comment">% b = [-1,zeros(1,16),32,zeros(1,16),1];</span>
    <span class="hljs-comment">% a = [1,1];</span>
    <span class="hljs-comment">% y3 = filter(b,a,y1)/32;</span>
    <span class="hljs-comment">%把信号差分来获取QRS波斜率信息，使用五点导数：y(nT)=(1/8T)[-x(nT-2T)-2x(nT-T)+2x(nT+T)+x(nT+2T)]</span>
    b = [<span class="hljs-number">-1</span>/<span class="hljs-number">4</span>,<span class="hljs-number">-1</span>/<span class="hljs-number">8</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>/<span class="hljs-number">8</span>,<span class="hljs-number">1</span>/<span class="hljs-number">4</span>];
    a = <span class="hljs-number">1</span>;
    y4 = filter(b,a,y3);
    <span class="hljs-comment">%对信号逐点平方，这使得所有数据点都为正，并且对强调较高频率(即，主要是ECG频率)的导数的输出进行非线性放大</span>
    y5 = y4.*y4; 
    <span class="hljs-comment">%滑动窗积分的目的是获得除了r波的斜率外的波形特征信息</span>
    b = [<span class="hljs-number">1</span> <span class="hljs-built_in">zeros</span>(<span class="hljs-number">1</span>,<span class="hljs-number">31</span>) <span class="hljs-number">-1</span>];
    a = [<span class="hljs-number">1</span>,<span class="hljs-number">-1</span>];
    y6 = filter(b,a,y5);

    <span class="hljs-comment">%开始画图了！</span>
    <span class="hljs-built_in">figure</span>; <span class="hljs-comment">%先画滤波效果</span>
    subplot(<span class="hljs-number">311</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));<span class="hljs-comment">%画图显示前2500个点</span>
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度（mV）'</span>);
    title(<span class="hljs-string">'原始信号'</span>);
    subplot(<span class="hljs-number">312</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y1(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); <span class="hljs-comment">%先低通</span>
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度（mV）'</span>);
    title(<span class="hljs-string">'低通滤波后的信号'</span>);
    subplot(<span class="hljs-number">313</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y3(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); <span class="hljs-comment">%再高通</span>
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度（mV）'</span>);
    title(<span class="hljs-string">'高通滤波后的信号'</span>);

    <span class="hljs-built_in">figure</span>; <span class="hljs-comment">%再画QRS检测的后续处理</span>
    subplot(<span class="hljs-number">311</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y4(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度'</span>);
    title(<span class="hljs-string">'做差分'</span>);
    subplot(<span class="hljs-number">312</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y5(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度'</span>);
    title(<span class="hljs-string">'做平方'</span>);
    subplot(<span class="hljs-number">313</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y6(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度'</span>);
    title(<span class="hljs-string">'做积分'</span>);
</div></code></pre>
<h3 id="2-%E7%BB%93%E6%9E%9C%E5%9B%BE%E7%89%87">2. 结果（图片）</h3>
<p><strong>低高通滤波结果</strong>
<img src="./../homework2/低高通滤波.png" alt="">
<strong>QRS波识别预处理结果</strong>
<img src="./../homework2/qrs识别预处理.png" alt=""> <div STYLE="page-break-after: always;"></div></p>
<h1 id="%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E7%9A%84%E6%BB%A4%E6%B3%A2%E9%A2%84%E5%A4%84%E7%90%86c%E8%AF%AD%E8%A8%80%E7%BC%96%E7%A8%8B">心电信号的滤波预处理——C语言编程</h1>
<h2 id="%E7%AC%AC%E4%B8%89%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第三次作业）</h2>
<h2 id="%E4%B8%80%E5%AF%B9%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E8%BF%9B%E8%A1%8C%E4%BD%8E%E9%80%9A%E6%BB%A4%E6%B3%A2%E5%92%8C%E9%AB%98%E9%80%9A%E6%BB%A4%E6%B3%A2%E7%9A%84%E7%A8%8B%E5%BA%8F">一、对心电信号进行低通滤波和高通滤波的程序</h2>
<h3 id="1%E6%BB%A4%E6%B3%A2">1.滤波</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"stdio.h"</span></span>

<span class="hljs-comment">//y(n)=2y(n-1)-y(n-2)+x(n)-2x(n-6)+x(n-12)</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">lpfilter</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> yn1,yn2,i,n,xn6,xn12=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> a[<span class="hljs-number">13</span>]={<span class="hljs-number">0</span>};

    <span class="hljs-comment">//环形缓冲区</span>
    <span class="hljs-keyword">if</span>(n&gt;=<span class="hljs-number">6</span>)
        xn6 = a[n<span class="hljs-number">-6</span>];
    <span class="hljs-keyword">else</span>
        xn6 = a[n+<span class="hljs-number">7</span>]; <span class="hljs-comment">// xn6 = a[13-(6-n)]</span>
    <span class="hljs-keyword">if</span>(n&lt;<span class="hljs-number">12</span>)
        xn12 = a[n+<span class="hljs-number">1</span>]; <span class="hljs-comment">//xn12 = a[13-(12-n)]</span>
    <span class="hljs-keyword">else</span> 
        xn12 = a[<span class="hljs-number">0</span>];

    a[n] = x;
    n++;
    <span class="hljs-keyword">if</span>(n==<span class="hljs-number">13</span>)
        n=<span class="hljs-number">0</span>;
    
    <span class="hljs-comment">//循环方式</span>
    <span class="hljs-comment">/*
    for(i=12; i&gt;0; i--)
    {
        a[i] = a[i-1];
    }
    a[0] = x;
    */</span>
    <span class="hljs-keyword">int</span> y = <span class="hljs-number">2</span>*yn1 - yn2 + x <span class="hljs-number">-2</span>*xn6 + xn12;
    yn2 = yn1;
    yn1 = y;

    y = y/<span class="hljs-number">36</span>; <span class="hljs-comment">//除去36倍低通滤波增益</span>
    <span class="hljs-keyword">return</span> y;
}

<span class="hljs-comment">//y(n)=y(n-1)+x(n)-x(n-32),z(n)=x(n-16)-y(n)/32 OR y(nT)=32x(nT-16T)-[y(nT-T)+x(nT)-x(nT-32T)]</span>
<span class="hljs-comment">//y(n)=y(n-1)+x(n)-x(n-32)</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">hpfilter</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> yn1,xn32,xn16,i,n=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> a[<span class="hljs-number">33</span>]={<span class="hljs-number">0</span>},b[<span class="hljs-number">17</span>]={<span class="hljs-number">0</span>};

    <span class="hljs-comment">//环形缓冲区</span>
    <span class="hljs-keyword">if</span>(n&lt;<span class="hljs-number">32</span>)
        xn32 = a[n+<span class="hljs-number">1</span>]; <span class="hljs-comment">//xn32 = a[32-(32-n)]</span>
    <span class="hljs-keyword">else</span> 
        xn32 = a[<span class="hljs-number">0</span>];
    a[n] = x;
    n++;
    <span class="hljs-keyword">if</span>(n==<span class="hljs-number">33</span>)
        n=<span class="hljs-number">0</span>;

    <span class="hljs-keyword">if</span>(i&lt;<span class="hljs-number">16</span>)
        xn16 = b[i+<span class="hljs-number">1</span>]; <span class="hljs-comment">//xn16 = a[16-(16-n)]</span>
    <span class="hljs-keyword">else</span> 
        xn16 = b[<span class="hljs-number">0</span>];
    b[i] = x;
    i++;
    <span class="hljs-keyword">if</span>(i==<span class="hljs-number">17</span>)
        i=<span class="hljs-number">0</span>;

    <span class="hljs-comment">//循环方式实现</span>
    <span class="hljs-comment">/*
    for(i=32; i&gt;0; i--)
    {
        a[i] = a[i-1];
    }
    a[0] = x;

    for(n=16; n&gt;0; n--)
    {
        b[n] = b[n-1];
    }
    b[0] = x;
    */</span>

    <span class="hljs-keyword">int</span> y = yn1 + x -xn32;
    yn1 = y;
    <span class="hljs-keyword">int</span> z = xn16 - y/<span class="hljs-number">32</span> ;  <span class="hljs-comment">//除去32倍的高通滤波增益</span>
    
    <span class="hljs-keyword">return</span> z;
}
</div></code></pre>
<h3 id="2%E4%B8%BB%E7%A8%8B%E5%BA%8F">2.主程序</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecgfilter.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecglib.h"</span></span>


<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{ 
    FILE *fp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100.dat"</span>,<span class="hljs-string">"rb"</span>);  <span class="hljs-comment">//只读模式读取原始文件</span>
    FILE *fp_lp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat"</span>,<span class="hljs-string">"wb"</span>);  <span class="hljs-comment">//只写模式</span>
    FILE *fp_hp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat"</span>,<span class="hljs-string">"wb"</span>);
    <span class="hljs-keyword">if</span>(fp == <span class="hljs-literal">NULL</span>) <span class="hljs-comment">//若不存在文件</span>
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"the file is not exist\r\n"</span>); 
    short x;
    <span class="hljs-keyword">while</span>(!feof(fp)) <span class="hljs-comment">//当文件没读完</span>
    {
        fread(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp); <span class="hljs-comment">//以1字节为单位读取2字节（short型长度）的数据放入x中</span>
        short y = lpfilter(x); <span class="hljs-comment">//逐点低通滤波</span>
        short z = hpfilter(y); <span class="hljs-comment">//逐点高通滤波</span>
        fwrite(&amp;y,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_lp);  <span class="hljs-comment">//将文件每两字节一个数据写入文件中</span>
        fwrite(&amp;z,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_hp);  
        <span class="hljs-comment">//printf("%d%d \r\n",i,x);</span>
    }
    
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello world\r\n"</span>);
    fclose(fp);  <span class="hljs-comment">//关闭文件，释放内存？</span>
    fclose(fp_lp);
    fclose(fp_hp);
    
    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;

}
</div></code></pre>
<h2 id="%E4%BA%8C%E4%BD%BF%E7%94%A8matlab%E8%AF%BB%E5%8F%96%E6%96%87%E4%BB%B6%E5%B9%B6%E6%98%BE%E7%A4%BA">二、使用MatLab读取文件并显示</h2>
<h3 id="1%E7%A8%8B%E5%BA%8F">1.程序</h3>
<pre class="hljs"><code><div>fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>
t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间  </span>

fid1 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg = fread(fid1,<span class="hljs-string">'short'</span>);
<span class="hljs-built_in">figure</span>;
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecgt(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-r'</span>);
fid2 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100.dat'</span>,<span class="hljs-string">'rb'</span>);
ecgt = fread(fid2,<span class="hljs-string">'short'</span>);
<span class="hljs-built_in">hold</span> on;
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-b'</span>);
fid3 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg2 = fread(fid3,<span class="hljs-string">'short'</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecg2(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-g'</span>);
<span class="hljs-built_in">legend</span>(<span class="hljs-string">'原始'</span>,<span class="hljs-string">'低通'</span>,<span class="hljs-string">'高通'</span>);
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'对比'</span>);

<span class="hljs-built_in">figure</span>;
subplot(<span class="hljs-number">311</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecgt(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'原始'</span>);
subplot(<span class="hljs-number">312</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'低通'</span>);
subplot(<span class="hljs-number">313</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg2(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'再高通'</span>);
</div></code></pre>
<h3 id="2%E7%BB%93%E6%9E%9C">2.结果</h3>
<p><img src="../homework3/没有对比就没有伤害.png" alt="">
<img src="../homework3/诶这才是真的对比.png" alt="">  <div STYLE="page-break-after: always;"></div></p>
<h1 id="%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E7%9A%84%E6%BB%A4%E6%B3%A2%E9%A2%84%E5%A4%84%E7%90%86c%E8%AF%AD%E8%A8%80%E7%BC%96%E7%A8%8B">心电信号的滤波预处理——C语言编程</h1>
<h2 id="%E7%AC%AC%E5%9B%9B%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第四次作业）</h2>
<h2 id="%E4%B8%80%E6%BB%A4%E6%B3%A2%E7%A8%8B%E5%BA%8F">一、滤波程序</h2>
<h3 id="1%E4%BD%8E%E9%80%9A%E6%BB%A4%E6%B3%A2">1.低通滤波</h3>
<pre class="hljs"><code><div><span class="hljs-comment">//y(n)=2y(n-1)-y(n-2)+x(n)-2x(n-6)+x(n-12)</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">lpfilter</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-comment">//真正的环形缓冲区</span>
    <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> LPBUFFERLENGTH 12  <span class="hljs-comment">//定义缓冲区长度</span></span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> yn1,yn2;
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> xn[LPBUFFERLENGTH],ptr=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">int</span> ptrhalf = ptr - LPBUFFERLENGTH/<span class="hljs-number">2</span>;
    <span class="hljs-keyword">if</span> (ptrhalf &lt;<span class="hljs-number">0</span>)
        ptrhalf = ptrhalf + LPBUFFERLENGTH;

    <span class="hljs-keyword">int</span> yn = <span class="hljs-number">2</span>* yn1 -yn2 + x - <span class="hljs-number">2</span>*xn[ptrhalf] + xn[ptr];

    xn[ptr] = x;
    ptr++;
    <span class="hljs-keyword">if</span> (ptr == LPBUFFERLENGTH)
        ptr = <span class="hljs-number">0</span>;

    yn2 = yn1;
    yn1 = yn;

    yn = yn/<span class="hljs-number">36</span>; <span class="hljs-comment">//低通滤波增益为36</span>
    <span class="hljs-keyword">return</span> yn;
}
</div></code></pre>
<h3 id="2%E9%AB%98%E9%80%9A%E6%BB%A4%E6%B3%A2">2.高通滤波</h3>
<pre class="hljs"><code><div><span class="hljs-comment">//y(n)=y(n-1)+x(n)-x(n-32),z(n)=x(n-16)-y(n)/32 OR y(nT)=32x(nT-16T)-[y(nT-T)+x(nT)-x(nT-32T)]</span>
<span class="hljs-comment">//y(n)=y(n-1)+x(n)-x(n-32)</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">hpfilter</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
   <span class="hljs-comment">//真正的环形缓冲区</span>
   <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> HPBUFFERLENGTH 32</span>
   <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> yn1;
   <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> xn[HPBUFFERLENGTH],ptr = <span class="hljs-number">0</span>;
   <span class="hljs-keyword">int</span> ptrhalf = ptr - HPBUFFERLENGTH/<span class="hljs-number">2</span>;
   <span class="hljs-keyword">if</span>(ptrhalf&lt;<span class="hljs-number">0</span>)
        ptrhalf = ptrhalf + HPBUFFERLENGTH;
    <span class="hljs-keyword">int</span> yn =yn1 + x - xn[ptr];
    yn1 = yn;
    <span class="hljs-keyword">int</span> z = xn[ptrhalf] - yn/<span class="hljs-number">32</span>;

    xn[ptr] = x;
    ptr ++;
    <span class="hljs-keyword">if</span>(ptr == HPBUFFERLENGTH)
    ptr = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">return</span> z;
}
</div></code></pre>
<h3 id="3%E5%B7%AE%E5%88%86">3.差分</h3>
<pre class="hljs"><code><div><span class="hljs-comment">//对信号差分</span>
<span class="hljs-comment">// y(nT) = (1/8T)[-x(nT-2T)-2x(nT-T)+2x(nT+T)+x(nT+2T)]</span>
<span class="hljs-comment">//y(n) = x(n)+2*x(n-1)-2*x(n-3)-x(n-4)</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">derivative</span> <span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span> 
</span>{
    <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> DERIV_LENGTH 4</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> ceof[<span class="hljs-number">5</span>] = {<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">0</span>,<span class="hljs-number">-2</span>,<span class="hljs-number">-1</span>};  <span class="hljs-comment">//系数矩阵</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> xn[DERIV_LENGTH],ptr = <span class="hljs-number">0</span>;  
    <span class="hljs-keyword">int</span> yn = ceof[<span class="hljs-number">0</span>]*x;
    <span class="hljs-keyword">int</span> p = ptr;
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i =DERIV_LENGTH ; i&gt;<span class="hljs-number">0</span>;i--)
    {
        yn = yn + ceof[i]*xn[p];  <span class="hljs-comment">//逐位相乘</span>
        p++;
        <span class="hljs-keyword">if</span>(p == DERIV_LENGTH)
            p = <span class="hljs-number">0</span>;
    }


    xn[ptr]= x; <span class="hljs-comment">//更新缓冲区最老的数据</span>
    ptr++; <span class="hljs-comment">//指针位移</span>
    <span class="hljs-keyword">if</span>(ptr == DERIV_LENGTH)  
        ptr = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">return</span> yn/<span class="hljs-number">8</span>;

}
</div></code></pre>
<h3 id="4%E7%A7%AF%E5%88%86">4.积分</h3>
<pre class="hljs"><code><div><span class="hljs-comment">//y(nT)=(1/N)[x(nT-(N-1)T)+x(nT-(N-2)T)+...+x(nT)] N=30 </span>
<span class="hljs-comment">//matlab: b =[1 zeros(1,31) -1]; a = [1,-1] / y(n) = y(n-1) + x(n) -x(n-32)</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">integration</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> INTEGRATION_LENGTH 32</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> yn1;
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> xn[INTEGRATION_LENGTH],ptr = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">int</span> yn = yn1 + x - xn[ptr];
    yn1 = yn;

    xn[ptr] = x;
    ptr++;
    <span class="hljs-keyword">if</span>(ptr == INTEGRATION_LENGTH)
        ptr = <span class="hljs-number">0</span>;
    
    <span class="hljs-keyword">return</span> yn;
}
</div></code></pre>
<h2 id="%E4%BA%8C%E4%B8%BB%E7%A8%8B%E5%BA%8F">二、主程序</h2>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecgfilter.h"</span></span>
 <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecglib.h"</span></span>


<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{ 
    FILE *fp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100.dat"</span>,<span class="hljs-string">"rb"</span>);  <span class="hljs-comment">//只读模式读取原始文件</span>
    FILE *fp_lp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat"</span>,<span class="hljs-string">"wb"</span>);  <span class="hljs-comment">//只写模式</span>
    FILE *fp_hp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat"</span>,<span class="hljs-string">"wb"</span>);
    FILE *fp_de = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-de.dat"</span>,<span class="hljs-string">"wb"</span>);
    FILE *fp_in = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-in.dat"</span>,<span class="hljs-string">"wb"</span>);
    <span class="hljs-keyword">if</span>(fp == <span class="hljs-literal">NULL</span>) <span class="hljs-comment">//若不存在文件</span>
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"the file is not exist\r\n"</span>); 
    short x;
    <span class="hljs-keyword">while</span>(!feof(fp)) <span class="hljs-comment">//当文件没读完</span>
    {
        
        fread(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp); <span class="hljs-comment">//以1字节为单位读取2字节（short型长度）的数据放入x中</span>
        short y = lpfilter(x); <span class="hljs-comment">//逐点低通滤波</span>
        short z = hpfilter(y); <span class="hljs-comment">//逐点高通滤波</span>
        short n = derivative(z); <span class="hljs-comment">//对信号进行差分</span>
        short m = integration(n*n); <span class="hljs-comment">//对信号平方后积分</span>
        fwrite(&amp;y,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_lp);  <span class="hljs-comment">//将文件每两字节一个数据写入文件中</span>
        fwrite(&amp;z,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_hp);  
        fwrite(&amp;n,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_de);
        fwrite(&amp;m,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_in);
        <span class="hljs-comment">//printf("%d%d \r\n",i,x);</span>
    }
    
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello world\r\n"</span>);
    fclose(fp);  <span class="hljs-comment">//关闭文件，释放内存？</span>
    fclose(fp_lp);
    fclose(fp_hp);
    fclose(fp_de);
    fclose(fp_in);

    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;

}
</div></code></pre>
<h2 id="%E4%B8%89matlab%E8%AF%BB%E5%8F%96%E5%B9%B6%E6%98%BE%E7%A4%BA">三、Matlab读取并显示</h2>
<h3 id="1%E7%A8%8B%E5%BA%8F">1.程序</h3>
<pre class="hljs"><code><div>fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>
  

fid2 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100.dat'</span>,<span class="hljs-string">'rb'</span>);
ecgt = fread(fid2,<span class="hljs-string">'short'</span>);
fid1 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg = fread(fid1,<span class="hljs-string">'short'</span>);
fid3 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg2 = fread(fid3,<span class="hljs-string">'short'</span>);
fid4 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-de.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg3 = fread(fid4,<span class="hljs-string">'short'</span>);
fid5 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-in.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg4 = fread(fid5,<span class="hljs-string">'short'</span>);

t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间</span>
<span class="hljs-built_in">figure</span>;
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecgt(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-r'</span>);
<span class="hljs-built_in">hold</span> on;
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-b'</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecg2(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-g'</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),ecg3(<span class="hljs-number">1</span>:<span class="hljs-number">1000</span>),<span class="hljs-string">'-m'</span>);
<span class="hljs-comment">%plot(t(1:1000),ecg4(1:1000),'--');</span>
<span class="hljs-built_in">legend</span>(<span class="hljs-string">'原始'</span>,<span class="hljs-string">'低通'</span>,<span class="hljs-string">'高通'</span>,<span class="hljs-string">'差分'</span>,<span class="hljs-string">'积分'</span>);
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'对比'</span>);

<span class="hljs-built_in">figure</span>;
subplot(<span class="hljs-number">311</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecgt(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'原始'</span>);
subplot(<span class="hljs-number">312</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'低通'</span>);
subplot(<span class="hljs-number">313</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg2(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'再高通'</span>);

<span class="hljs-built_in">figure</span>;
subplot(<span class="hljs-number">211</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg3(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'差分'</span>);
subplot(<span class="hljs-number">212</span>);
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg4(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'积分'</span>);
</div></code></pre>
<h3 id="2%E7%BB%93%E6%9E%9C">2.结果</h3>
<p><img src="../homework4/全体.png" alt="">
<img src="../homework4/分开.png" alt="">
<img src="../homework4/再分开.png" alt=""> <div STYLE="page-break-after: always;"></div></p>
<h1 id="%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E7%9A%84qrs%E6%A3%80%E6%B5%8Bc%E8%AF%AD%E8%A8%80%E7%BC%96%E7%A8%8B">心电信号的QRS检测——C语言编程</h1>
<h2 id="%E7%AC%AC%E4%BA%94%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第五次作业）</h2>
<pre><code class="language-mermaid"><div class="mermaid">stateDiagram-v2
    超过阈值-->低于阈值:寻找下降沿
    低于阈值-->超过阈值:寻找上升沿
    超过阈值-->结果:输出
    低于阈值-->结果:输出
</div></code></pre>
<h2 id="%E4%B8%80%E6%BB%A4%E6%B3%A2%E5%90%8E%E6%A3%80%E6%B5%8Bqrs%E6%B3%A2">一、滤波后检测QRS波</h2>
<h3 id="1%E4%B8%BB%E7%A8%8B%E5%BA%8F">1.主程序</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecgfilter.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecglib.h"</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{ 
    FILE *fp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100.dat"</span>,<span class="hljs-string">"rb"</span>);  <span class="hljs-comment">//只读模式读取原始文件</span>
    FILE *fp_po = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-po.dat"</span>,<span class="hljs-string">"wb"</span>);


    <span class="hljs-keyword">if</span>(fp == <span class="hljs-literal">NULL</span>) <span class="hljs-comment">//若不存在文件</span>
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"the file is not exist\r\n"</span>); 
    short x;
    <span class="hljs-keyword">int</span> icount = <span class="hljs-number">0</span>;  <span class="hljs-comment">//位置变量</span>
    <span class="hljs-keyword">while</span>(!feof(fp)) <span class="hljs-comment">//当文件没读完</span>
    {
        fread(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp); <span class="hljs-comment">//以1字节为单位读取2字节（short型长度）的数据放入x中</span>
        short qrs = qrsdetect(x);  <span class="hljs-comment">//检测当前位置是否正在发生qrs波</span>
        icount ++;  <span class="hljs-comment">//位置的移动？</span>
        <span class="hljs-keyword">if</span> (qrs==<span class="hljs-number">1</span> || qrs ==<span class="hljs-number">0</span>)  <span class="hljs-comment">//将上升超过阈值时和下降低于阈值时的点存入文件</span>
             fwrite(&amp;icount,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>),fp_po);  <span class="hljs-comment">//把位置存到文件里？</span>
    }
    
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello world\r\n"</span>);
    fclose(fp);  <span class="hljs-comment">//关闭文件，释放内存？</span>
    fclose(fp_po);

    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;

}
</div></code></pre>
<h3 id="2%E8%AF%86%E5%88%AB%E5%87%BD%E6%95%B0qrsdetect">2.识别函数qrsdetect</h3>
<pre class="hljs"><code><div><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsdetect</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">int</span> y = qrsfilter(x);  <span class="hljs-comment">//先滤波得到积分后的方波</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//初始是0状态</span>
    <span class="hljs-keyword">int</span> thx = <span class="hljs-number">2000</span>;  <span class="hljs-comment">//设一个阈值 </span>

    <span class="hljs-keyword">switch</span> (star)
    {
    <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:  <span class="hljs-comment">//0状态的时候 寻找上升支？</span>
        <span class="hljs-keyword">if</span>(y&gt;thx)  <span class="hljs-comment">//超过阈值（上升支）</span>
        {
            star = <span class="hljs-number">1</span>;  <span class="hljs-comment">//切换为1状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <span class="hljs-comment">//获得上升沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:  <span class="hljs-comment">//1状态的时候 寻找下降支？</span>
        <span class="hljs-keyword">if</span>(y&lt;thx)  <span class="hljs-comment">//低于阈值（下降支）</span>
        {
            star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//切换为0状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;  <span class="hljs-comment">//获得下降沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;  <span class="hljs-comment">//其他时刻返回9 </span>
}
</div></code></pre>
<h3 id="3%E6%BB%A4%E6%B3%A2%E5%87%BD%E6%95%B0qrsfilter">3.滤波函数qrsfilter</h3>
<pre class="hljs"><code><div>FILE *fp_lp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat"</span>,<span class="hljs-string">"wb"</span>);  <span class="hljs-comment">//只写模式</span>
FILE *fp_hp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat"</span>,<span class="hljs-string">"wb"</span>);
FILE *fp_de = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-de.dat"</span>,<span class="hljs-string">"wb"</span>);
FILE *fp_in = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-in.dat"</span>,<span class="hljs-string">"wb"</span>);
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsfilter</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-comment">//也还想要获得每一步的结果试试</span>
    x = lpfilter(x);
    fwrite(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_lp);  <span class="hljs-comment">//将文件每两字节一个数据写入文件中</span>
    x = hpfilter(x);
    fwrite(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_hp);  
    x = derivative(x);
    fwrite(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_de);
    x = integration(x*x);
    fwrite(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp_in);
    <span class="hljs-keyword">return</span> x;
}
</div></code></pre>
<h2 id="%E4%BA%8Cqrs%E6%B3%A2%E6%A3%80%E6%B5%8B%E7%BB%93%E6%9E%9C%E7%9A%84%E6%98%BE%E7%A4%BA">二、QRS波检测结果的显示</h2>
<h3 id="1matlab%E7%A8%8B%E5%BA%8F">1.Matlab程序</h3>
<pre class="hljs"><code><div>fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>

fid2 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100.dat'</span>,<span class="hljs-string">'rb'</span>);
ecgt = fread(fid2,<span class="hljs-string">'short'</span>);
fid1 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg = fread(fid1,<span class="hljs-string">'short'</span>);
fid3 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg2 = fread(fid3,<span class="hljs-string">'short'</span>);
fid4 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-de.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg3 = fread(fid4,<span class="hljs-string">'short'</span>);
fid5 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-in.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg4 = fread(fid5,<span class="hljs-string">'short'</span>);
fid6 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-po.dat'</span>,<span class="hljs-string">'rb'</span>);
qrs = fread(fid6,<span class="hljs-string">'int'</span>);
<span class="hljs-comment">%%</span>
<span class="hljs-comment">%画找的位置在原始波形上的显示</span>
<span class="hljs-built_in">figure</span>;
t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间</span>
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">50</span>)),ecgt(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">50</span>)));
<span class="hljs-built_in">hold</span> on;
<span class="hljs-built_in">plot</span>(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">50</span>)/fs,ecgt(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">50</span>)),<span class="hljs-string">'.r'</span>);
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'找到的位置显示在原波形图上'</span>);
<span class="hljs-comment">%%</span>
<span class="hljs-comment">%画找的位置在积分图上的显示</span>
<span class="hljs-built_in">figure</span>;
t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间</span>
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">50</span>)),ecg4(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">50</span>)));
<span class="hljs-built_in">hold</span> on;
<span class="hljs-built_in">plot</span>(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">50</span>)/fs,ecg4(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">50</span>)),<span class="hljs-string">'.r'</span>);
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'找到的位置显示在积分图上'</span>);
</div></code></pre>
<h3 id="2%E7%BB%93%E6%9E%9C">2.结果</h3>
<p><img src="../homework5/1.png" alt="">
<img src="../homework5/2.png" alt=""> <div STYLE="page-break-after: always;"></div></p>
<h1 id="%E6%A3%80%E6%B5%8B%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E7%9A%84qrs%E5%B9%B6%E8%BE%93%E5%87%BA%E5%BF%83%E7%8E%87">检测心电信号的QRS并输出心率</h1>
<h2 id="%E7%AC%AC%E5%85%AD%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第六次作业）</h2>
<h2 id="%E4%B8%80%E9%80%9A%E8%BF%87%E8%8E%B7%E5%BE%97%E7%9A%84qrs%E6%B3%A2%E8%AE%A1%E7%AE%97%E5%BF%83%E7%8E%87%E5%B9%B6%E6%AF%8F%E7%A7%92%E8%BE%93%E5%87%BA%E4%B8%80%E6%AC%A1">一、通过获得的QRS波计算心率并每秒输出一次</h2>
<h3 id="1%E4%B8%BB%E7%A8%8B%E5%BA%8F">1.主程序</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecgfilter.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecglib.h"</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{ 
    FILE *fp = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100.dat"</span>,<span class="hljs-string">"rb"</span>);  <span class="hljs-comment">//只读模式读取原始文件</span>
    FILE *fp_po = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-po.dat"</span>,<span class="hljs-string">"wb"</span>);
    FILE *fp_hr = fopen(<span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/100-hr.dat"</span>,<span class="hljs-string">"wb"</span>);


    <span class="hljs-keyword">if</span>(fp == <span class="hljs-literal">NULL</span>) <span class="hljs-comment">//若不存在文件</span>
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"the file is not exist\r\n"</span>); 
    short x;
    <span class="hljs-keyword">while</span>(!feof(fp)) <span class="hljs-comment">//当文件没读完</span>
    {
        fread(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp); <span class="hljs-comment">//以1字节为单位读取2字节（short型长度）的数据放入x中</span>
        <span class="hljs-keyword">int</span> evt = PushData(x);  <span class="hljs-comment">//获得心率和QRS波位置</span>
      
        <span class="hljs-keyword">if</span>(evt&amp;EVENT_1sec)  <span class="hljs-comment">//每过一秒</span>
        {
            <span class="hljs-keyword">int</span> hr =GetCurHr();  <span class="hljs-comment">//返回心率值</span>
            fwrite(&amp;hr,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>),fp_hr);
        }
        <span class="hljs-keyword">if</span> (evt&amp;EVENT_QRS)  <span class="hljs-comment">//每检测到QRS波的时候</span>
        {
            <span class="hljs-keyword">int</span> po = GetQRSPos();  <span class="hljs-comment">//返回QRS波位置</span>
            fwrite(&amp;po,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>),fp_po);
        }
    }
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello world\r\n"</span>);
    fclose(fp);  <span class="hljs-comment">//关闭文件，释放内存？</span>
    fclose(fp_po);
    fclose(fp_hr);

    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;
}
</div></code></pre>
<h3 id="2%E8%AE%A1%E7%AE%97%E5%BF%83%E7%8E%87%E5%B9%B6%E6%98%BE%E7%A4%BA">2.计算心率并显示</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecglib.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecgfilter.h"</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> curHR = <span class="hljs-number">-1</span> ;  <span class="hljs-comment">//初始化心率为负</span>
<span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> curQRSPos = <span class="hljs-number">0</span> ;  <span class="hljs-comment">//初始QRS波位置为0</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> RRBUF_LEN 8  <span class="hljs-comment">//获得八个波形的平均心率</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">PushData</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> rrbuf[RRBUF_LEN] = {<span class="hljs-number">0</span>};  <span class="hljs-comment">//初始化环形缓冲区</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> icount = <span class="hljs-number">0</span>;  <span class="hljs-comment">//寻找位置的变量</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> ptr = <span class="hljs-number">0</span>;  <span class="hljs-comment">//ringbuffer的“指针”</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> last_po = <span class="hljs-number">0</span>;  <span class="hljs-comment">//暂存上一个QRS波的位置</span>
    <span class="hljs-keyword">int</span> evt = <span class="hljs-number">0</span>;  <span class="hljs-comment">//定义状态</span>
    short qrs = qrsdetect(x);
    <span class="hljs-keyword">if</span>(icount%<span class="hljs-number">250</span> == <span class="hljs-number">0</span>) <span class="hljs-comment">//从第零秒开始输出，每秒输出一次</span>
    {
        evt = evt|<span class="hljs-number">0X02</span>;  <span class="hljs-comment">//按位或10</span>
    }
    <span class="hljs-keyword">if</span>(qrs == <span class="hljs-number">1</span>)  <span class="hljs-comment">//当下降沿低于阈值的时候</span>
    {
        evt = evt|<span class="hljs-number">0X01</span>;  <span class="hljs-comment">//按位或01</span>
        curQRSPos = icount;  <span class="hljs-comment">//将下降沿低于阈值的点的位置给全局变量</span>

      
        <span class="hljs-keyword">if</span>(last_po !=<span class="hljs-number">0</span>)  <span class="hljs-comment">//从第二个和第一个点的心率开始</span>
        {
            rrbuf[ptr] = (<span class="hljs-keyword">int</span>)<span class="hljs-number">60</span>*<span class="hljs-number">250.0</span>/((icount-last_po)); <span class="hljs-comment">//将通过一个RR间期计算出的心率放入数组，没搞懂的小数点和int</span>
            last_po = icount; <span class="hljs-comment">//把当前位置寄存</span>
            ptr++;  <span class="hljs-comment">//指针移动</span>
            <span class="hljs-keyword">if</span> (ptr == RRBUF_LEN )  <span class="hljs-comment">//环形缓冲区</span>
            {
                ptr = <span class="hljs-number">0</span>;
            }
        }
        <span class="hljs-keyword">else</span>
            last_po = icount; <span class="hljs-comment">//把第一个点的位置赋值</span>
        
        <span class="hljs-keyword">int</span> sum = <span class="hljs-number">0</span>;  
        <span class="hljs-keyword">int</span> n = <span class="hljs-number">0</span> ;  <span class="hljs-comment">//方便的方式计算数组未满时的数组均值</span>
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> s = <span class="hljs-number">0</span>; s&lt;RRBUF_LEN; s++)
        {   
            <span class="hljs-keyword">if</span>(rrbuf[s] &gt; <span class="hljs-number">0</span> )
            {
                sum = sum + rrbuf[s];  <span class="hljs-comment">//计算数组总体心率</span>
                n ++;
            }
        }
        <span class="hljs-keyword">if</span>(n != <span class="hljs-number">0</span>)
            curHR = sum/n;  <span class="hljs-comment">//数组有数据时求平均</span>
        <span class="hljs-keyword">else</span>
            curHR = <span class="hljs-number">-1</span> ;  <span class="hljs-comment">//当数组为空时（没有心率时）输出-1</span>
    }
    icount ++;
    <span class="hljs-keyword">return</span> evt;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">GetCurHr</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">return</span>  curHR;  <span class="hljs-comment">//返回心率</span>
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">GetQRSPos</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">return</span> curQRSPos;  <span class="hljs-comment">//返回QRS位置</span>
}
</div></code></pre>
<h2 id="%E4%BA%8C%E7%94%BB%E5%9B%BE%E5%B1%95%E7%A4%BA%E6%AF%8F%E7%A7%92%E8%BE%93%E5%87%BA%E7%9A%84%E5%BF%83%E7%8E%87">二、画图展示每秒输出的心率</h2>
<h3 id="1matlab%E7%A8%8B%E5%BA%8F">1.Matlab程序</h3>
<pre class="hljs"><code><div>fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>
  
fid2 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100.dat'</span>,<span class="hljs-string">'rb'</span>);
ecgt = fread(fid2,<span class="hljs-string">'short'</span>);
fid1 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-lp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg = fread(fid1,<span class="hljs-string">'short'</span>);
fid3 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-hp.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg2 = fread(fid3,<span class="hljs-string">'short'</span>);
fid4 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-de.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg3 = fread(fid4,<span class="hljs-string">'short'</span>);
fid5 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-in.dat'</span>,<span class="hljs-string">'rb'</span>);
ecg4 = fread(fid5,<span class="hljs-string">'short'</span>);
fid6 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-po.dat'</span>,<span class="hljs-string">'rb'</span>);
qrs = fread(fid6,<span class="hljs-string">'int'</span>);
fid7 = fopen(<span class="hljs-string">'G:/sss/SmartHealth-master/DATA/mitdb/100-hr.dat'</span>,<span class="hljs-string">'rb'</span>);
hr = fread(fid7,<span class="hljs-string">'int'</span>);
<span class="hljs-comment">%%</span>
<span class="hljs-built_in">figure</span>;
<span class="hljs-built_in">plot</span>(hr(<span class="hljs-number">1</span>:<span class="hljs-number">500</span>));
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'心率'</span>);
title(<span class="hljs-string">'心率每秒的变化'</span>);
<span class="hljs-comment">%%</span>
<span class="hljs-comment">%画找的位置在原始波形上的显示</span>
<span class="hljs-built_in">figure</span>;
t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间</span>
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">49</span>)),ecgt(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">49</span>)));
<span class="hljs-built_in">hold</span> on;
<span class="hljs-built_in">plot</span>(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">49</span>)/fs,ecgt(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">49</span>)),<span class="hljs-string">'.r'</span>);
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'找到的位置显示在原波形图上'</span>);
<span class="hljs-comment">%%</span>
<span class="hljs-comment">%画找的位置在积分图上的显示</span>
<span class="hljs-built_in">figure</span>;
t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间</span>
<span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">49</span>)),ecg4(<span class="hljs-number">1</span>:qrs(<span class="hljs-number">49</span>)));
<span class="hljs-built_in">hold</span> on;
<span class="hljs-built_in">plot</span>(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">49</span>)/fs,ecg4(qrs(<span class="hljs-number">1</span>:<span class="hljs-number">49</span>)),<span class="hljs-string">'.r'</span>);
xlabel(<span class="hljs-string">'时间（s）'</span>);
ylabel(<span class="hljs-string">'幅度'</span>);
title(<span class="hljs-string">'找到的位置显示在积分图上'</span>);
</div></code></pre>
<h3 id="2%E7%BB%93%E6%9E%9C">2.结果</h3>
<p><img src="../homework6/HeartRate.jpg" alt="">
<img src="../homework6/QRSdetect1.jpg" alt="">
<img src="../homework6/QRSdetect2.jpg" alt="">    <div STYLE="page-break-after: always;"></div></p>
<h1 id="%E8%AE%A1%E7%AE%97%E6%A3%80%E6%B5%8B%E5%87%BA%E7%9A%84qrs%E7%9A%84%E6%95%8F%E6%84%9F%E5%BA%A6qrs-sensitivity%E5%92%8C%E9%98%B3%E6%80%A7%E9%A2%84%E6%B5%8B%E5%80%BCqrs-positive-predictivity">计算检测出的QRS的敏感度（QRS sensitivity）和阳性预测值（QRS positive predictivity）</h1>
<h2 id="%E7%AC%AC%E4%B8%83%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第七次作业）</h2>
<h2 id="%E4%B8%80%E6%A3%80%E6%B5%8Bqrs%E6%B3%A2%E9%80%BB%E8%BE%91%E7%9A%84%E6%94%B9%E8%BF%9B">一、检测QRS波逻辑的改进</h2>
<h3 id="1%E5%9B%BA%E5%AE%9A%E9%98%88%E5%80%BC%E6%B3%95%E9%98%88%E5%80%BC%E8%AE%BE%E4%B8%BA2000">1.固定阈值法（阈值设为2000）</h3>
<pre class="hljs"><code><div><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsdetect</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">int</span> y = qrsfilter(x);  <span class="hljs-comment">//先滤波得到积分后的方波</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//初始是0状态</span>
    <span class="hljs-keyword">int</span> thx = <span class="hljs-number">2000</span>;  <span class="hljs-comment">//设一个阈值 </span>

    <span class="hljs-keyword">switch</span> (star)
    {
    <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:  <span class="hljs-comment">//0状态的时候 寻找上升支？</span>
        <span class="hljs-keyword">if</span>(y&gt;thx)  <span class="hljs-comment">//超过阈值（上升支）</span>
        {
            star = <span class="hljs-number">1</span>;  <span class="hljs-comment">//切换为1状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <span class="hljs-comment">//获得上升沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:  <span class="hljs-comment">//1状态的时候 寻找下降支？</span>
        <span class="hljs-keyword">if</span>(y&lt;thx)  <span class="hljs-comment">//低于阈值（下降支）</span>
        {
            star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//切换为0状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;  <span class="hljs-comment">//获得下降沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;  <span class="hljs-comment">//其他时刻返回9 </span>
}
</div></code></pre>
<h3 id="2%E6%94%B9%E5%8F%98%E9%98%88%E5%80%BC%E6%B3%95%E6%9C%80%E5%A4%A7%E5%80%BC%E7%B3%BB%E6%95%B0%E4%B8%BA02">2.改变阈值法（最大值系数为0.2）</h3>
<pre class="hljs"><code><div><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsdetect</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> y = qrsfilter(x);  <span class="hljs-comment">//先滤波得到积分后的方波</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//初始是0状态</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;
    <span class="hljs-keyword">int</span> <span class="hljs-built_in">max</span> = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">if</span>(a &lt; <span class="hljs-number">8</span>)  <span class="hljs-comment">//取前八个周期</span>
    { 
        <span class="hljs-keyword">if</span> (i&lt;<span class="hljs-number">500</span>)
        {
            thr[i] = y;
            i++;
        }
        <span class="hljs-keyword">else</span>  <span class="hljs-comment">//当取了两秒后</span>
        {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> s= <span class="hljs-number">0</span>; s &lt; <span class="hljs-number">500</span>; s++)
            {
                <span class="hljs-keyword">if</span> (thr[s]&gt;<span class="hljs-built_in">max</span>)
                {
                    <span class="hljs-built_in">max</span> = thr[s];  <span class="hljs-comment">//找这两秒内的最大值</span>
                }
            }
            su = su + <span class="hljs-built_in">max</span>;  <span class="hljs-comment">//累加最大值</span>
            thx = (su * <span class="hljs-number">0.2</span>)/(a+<span class="hljs-number">1</span>);  <span class="hljs-comment">//求平均，乘阈值系数0.2</span>
            i=<span class="hljs-number">0</span>;
            a++;
        }
    }
    <span class="hljs-keyword">switch</span> (star)
    {
    <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:  <span class="hljs-comment">//0状态的时候 寻找上升支？</span>
        <span class="hljs-keyword">if</span>(y&gt;thx)  <span class="hljs-comment">//超过阈值（上升支）</span>
        {
            star = <span class="hljs-number">1</span>;  <span class="hljs-comment">//切换为1状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  
        }
        <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:  <span class="hljs-comment">//1状态的时候 寻找下降支？</span>
        <span class="hljs-keyword">if</span>(y&lt;thx)  <span class="hljs-comment">//低于阈值（下降支）</span>
        {
            star = <span class="hljs-number">0</span>;  <span class="hljs-comment">//切换为0状态</span>
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;  <span class="hljs-comment">//获得下降沿的阈值临界点</span>
        }
        <span class="hljs-keyword">break</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <span class="hljs-comment">//其他时刻返回9 </span>
}
</div></code></pre>
<h2 id="3%E5%9C%A8%E8%87%AA%E9%80%82%E5%BA%94%E9%98%88%E5%80%BC%E5%9F%BA%E7%A1%80%E4%B8%8A%E8%B0%83%E5%8F%82%E5%8A%A0%E5%85%A5%E5%BB%B6%E6%97%B6%E9%98%B2%E8%AF%AF%E5%88%A4%E6%94%B9%E5%8F%98%E5%91%A8%E6%9C%9F">3.在自适应阈值基础上调参+加入延时防误判+改变周期</h2>
<h3 id="%E9%98%88%E5%80%BC%E7%B3%BB%E6%95%B0015%E9%98%88%E5%80%BC%E8%AE%BE%E4%B8%BA%E5%89%8D%E5%85%AB%E7%A7%92%E5%86%85%E5%85%AB%E4%B8%AA%E6%9C%80%E5%A4%A7%E5%80%BC%E7%9A%84%E4%B8%AD%E4%BD%8D%E6%95%B0%E9%98%88%E5%80%BC%E7%B3%BB%E6%95%B0">阈值系数0.15；阈值设为前八秒内八个最大值的中位数*阈值系数</h3>
<h3 id="%E6%AF%8F%E6%AC%A1%E5%BB%B6%E6%97%B620%E4%B8%AA%E7%82%B9%E5%86%8D%E5%88%87%E6%8D%A2%E5%AF%BB%E6%89%BE%E4%B8%8A%E5%8D%87%E6%94%AF%E4%B8%8B%E9%99%8D%E6%94%AF%E7%8A%B6%E6%80%81%E5%91%A8%E6%9C%9F%E5%8F%98%E4%B8%BA%E4%B8%80%E7%A7%92">每次延时20个点再切换寻找上升支/下降支状态；周期变为一秒</h3>
<pre class="hljs"><code><div><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">qrsdetect</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span>
</span>{
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> y = qrsfilter(x); <span class="hljs-comment">//先滤波得到积分后的方波</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> star = <span class="hljs-number">1</span>;           <span class="hljs-comment">//初始是1状态</span>
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">int</span> <span class="hljs-built_in">max</span> = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">if</span> (a &lt; <span class="hljs-number">8</span>) <span class="hljs-comment">//取前八个周期</span>
        {
            <span class="hljs-keyword">if</span> (i &lt; <span class="hljs-number">250</span>)
            {
                thr[i] = y;
                i++;
            }
            <span class="hljs-keyword">else</span> <span class="hljs-comment">//当取了一秒后</span>
            {
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> s = <span class="hljs-number">0</span>; s &lt; <span class="hljs-number">250</span>; s++)
                {
                    <span class="hljs-keyword">if</span> (thr[s] &gt; <span class="hljs-built_in">max</span>)
                    {
                        <span class="hljs-built_in">max</span> = thr[s]; <span class="hljs-comment">//找这一秒内的最大值</span>
                    }
                }
                maxm[a] = <span class="hljs-built_in">max</span>;
                i = <span class="hljs-number">0</span>;
                a++;
                su = su + <span class="hljs-built_in">max</span>;              <span class="hljs-comment">//累加最大值</span>
                thx = (su * <span class="hljs-number">0.15</span>) / (a + <span class="hljs-number">1</span>); <span class="hljs-comment">//求平均，乘阈值系数0.15</span>
            }
        }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(a == <span class="hljs-number">8</span>)
        {
            thx = mid(maxm, a) * <span class="hljs-number">0.15</span>;  <span class="hljs-comment">//找中位数，乘阈值系数0.15</span>
            a = <span class="hljs-number">9</span>;
        }
    <span class="hljs-keyword">switch</span> (star)
    {
        <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:          <span class="hljs-comment">//1状态的时候 寻找上升支？</span>
            <span class="hljs-keyword">if</span> (y &gt; thx) <span class="hljs-comment">//超过阈值（上升支）</span>
            {
                pre = <span class="hljs-number">0</span>;
                star = <span class="hljs-number">4</span>; <span class="hljs-comment">//切换为4状态</span>
            }
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:          <span class="hljs-comment">//2状态的时候 寻找下降支？</span>
            <span class="hljs-keyword">if</span> (y &lt; thx) <span class="hljs-comment">//低于阈值（下降支）</span>
            {
                pre = <span class="hljs-number">0</span>;
                star = <span class="hljs-number">3</span>; <span class="hljs-comment">//切换为3状态</span>
                <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>; <span class="hljs-comment">//获得下降沿的阈值临界点</span>
            }
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>: <span class="hljs-comment">//这里是不是在防止误判？隔20个点后再寻找上升沿</span>
            pre++;
            <span class="hljs-keyword">if</span> (pre == <span class="hljs-number">20</span>)
            {
                pre = <span class="hljs-number">0</span>;
                star = <span class="hljs-number">1</span>;  <span class="hljs-comment">//去寻找上升支</span>
            }
            <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">4</span>:  <span class="hljs-comment">//防止误判 隔20个点后再寻找下降沿</span>
            pre++;
            <span class="hljs-keyword">if</span> (pre == <span class="hljs-number">20</span>)
            {
                pre = <span class="hljs-number">0</span>;
                star = <span class="hljs-number">2</span>;  <span class="hljs-comment">//去寻找下降支</span>
            }
        }
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; <span class="hljs-comment">//其他时刻返回9</span>
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">mid</span><span class="hljs-params">(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> a[], <span class="hljs-keyword">int</span> x)</span> <span class="hljs-comment">//找中位数</span>
</span>{
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> b[x];
    <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> median;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; x - <span class="hljs-number">1</span>; ++i)  <span class="hljs-comment">//排序</span>
    {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt; x - i - <span class="hljs-number">1</span>; ++j)
        {
            <span class="hljs-keyword">if</span> (a[j] &gt; a[j + <span class="hljs-number">1</span>])
            {
                <span class="hljs-keyword">int</span> t = a[j];
                a[j] = a[j + <span class="hljs-number">1</span>];
                a[j + <span class="hljs-number">1</span>] = t;
            }
        }
    }
    <span class="hljs-keyword">if</span> (x % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>)
        median = (a[(x / <span class="hljs-number">2</span>) - <span class="hljs-number">1</span>] + a[x / <span class="hljs-number">2</span>]) / <span class="hljs-number">2</span>;  <span class="hljs-comment">//偶数时的中位数 </span>
    <span class="hljs-keyword">else</span>
        median = a[(x - <span class="hljs-number">1</span>) / <span class="hljs-number">2</span>];  <span class="hljs-comment">//奇数时的中位数</span>
    <span class="hljs-keyword">return</span> median; 
}
</div></code></pre>
<h2 id="%E4%BA%8C%E6%A3%80%E6%B5%8B%E7%A8%8B%E5%BA%8F">二、检测程序</h2>
<h3 id="1%E4%B8%BB%E7%A8%8B%E5%BA%8F%E8%BE%93%E5%87%BAate%E6%96%87%E4%BB%B6">1.主程序——输出ate文件</h3>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecgfilter.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"ecglib.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"wfdb/wfdb.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"wfdb/ecgmap.h"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> delay 40 <span class="hljs-comment">//滤波延时</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc , <span class="hljs-keyword">char</span> *argv[])</span>
</span>{ 
    <span class="hljs-keyword">char</span> *record = argv[<span class="hljs-number">1</span>];

    <span class="hljs-comment">//wfdb库对ate文件的设定初始化</span>
    WFDB_Anninfo an[<span class="hljs-number">2</span>]; 
    WFDB_Annotation annot;
    an[<span class="hljs-number">0</span>].name = <span class="hljs-string">"ate"</span>;
    an[<span class="hljs-number">0</span>].stat = WFDB_WRITE;

    <span class="hljs-comment">//设定路径</span>
    <span class="hljs-keyword">char</span> path[<span class="hljs-number">200</span>] = <span class="hljs-string">"G:/sss/SmartHealth-master/DATA/mitdb/"</span>;
    setwfdb(path);
    <span class="hljs-keyword">char</span> fullname[<span class="hljs-number">200</span>];
    <span class="hljs-built_in">sprintf</span>(fullname,<span class="hljs-string">"%s%s.dat"</span>,path,record); <span class="hljs-comment">//字符串合并</span>
    FILE *fp = fopen(fullname,<span class="hljs-string">"rb"</span>); <span class="hljs-comment">//读文件</span>

    <span class="hljs-comment">//FILE *fp = fopen("G:/sss/SmartHealth-master/DATA/mitdb/100.dat","rb");</span>

    <span class="hljs-keyword">if</span>(fp == <span class="hljs-literal">NULL</span>) <span class="hljs-comment">//若不存在文件</span>
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"file open error"</span>); 
    
    <span class="hljs-keyword">if</span> (annopen(record, an, <span class="hljs-number">1</span>) &lt; <span class="hljs-number">0</span>) <span class="hljs-comment">//不知道在干什么</span>
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"annopen error"</span>);
        <span class="hljs-built_in">exit</span>(<span class="hljs-number">1</span>);
    }

    short x;
    <span class="hljs-keyword">while</span>(!feof(fp)) <span class="hljs-comment">//当文件没读完</span>
    {
        fread(&amp;x,<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(short),fp); <span class="hljs-comment">//以1字节为单位读取2字节（short型长度）的数据放入x中</span>
        <span class="hljs-keyword">int</span> evt = PushData(x);  <span class="hljs-comment">//获得心率和QRS波位置</span>
      
        <span class="hljs-keyword">if</span>(evt&amp;EVENT_1sec)  <span class="hljs-comment">//每过一秒</span>
        {
            <span class="hljs-keyword">int</span> hr =GetCurHr();  <span class="hljs-comment">//返回心率值</span>
            <span class="hljs-comment">//fwrite(&amp;hr,1,sizeof(int),fp_hr);</span>
        }
        <span class="hljs-keyword">if</span> (evt&amp;EVENT_QRS)  <span class="hljs-comment">//每检测到QRS波的时候</span>
        {
            <span class="hljs-keyword">int</span> po = GetQRSPos();  <span class="hljs-comment">//返回QRS波位置</span>
            <span class="hljs-comment">//fwrite(&amp;po,1,sizeof(int),fp_po);</span>
            <span class="hljs-comment">//又是ate文件的设置</span>
            annot.anntyp = NORMAL;
            annot.time = po - <span class="hljs-built_in">delay</span> - <span class="hljs-number">20</span>;  <span class="hljs-comment">//20是第三种方法的延时</span>
            annot.aux = <span class="hljs-literal">NULL</span>;
            putann(<span class="hljs-number">0</span>,&amp;annot);
        }
    }
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello world\r\n"</span>);
    fclose(fp);  <span class="hljs-comment">//关闭文件，释放内存？</span>
    oannclose(<span class="hljs-number">0</span>);  <span class="hljs-comment">//还是库文件的关闭？</span>
    <span class="hljs-comment">// fclose(fp_po);</span>
    <span class="hljs-comment">// fclose(fp_hr);</span>

    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;
}
</div></code></pre>
<h3 id="2%E9%80%9A%E8%BF%87matlab%E8%BE%93%E5%87%BA%E5%B9%B6%E8%B0%83%E7%94%A8bxb%E5%87%BD%E6%95%B0">2.通过Matlab输出并调用bxb函数</h3>
<pre class="hljs"><code><div>flist = dir(<span class="hljs-string">'*.dat'</span>);
<span class="hljs-keyword">for</span> i1=<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(flist)
    a = flist(i1).name;
    record = a(<span class="hljs-number">1</span>:<span class="hljs-keyword">end</span><span class="hljs-number">-4</span>);
    cmd = sprintf(<span class="hljs-string">'text %s'</span>,record);
    system(cmd);
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">for</span> i1=<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(flist)
    a = flist(i1).name;
    record = a(<span class="hljs-number">1</span>:<span class="hljs-keyword">end</span><span class="hljs-number">-4</span>);
    cmd = sprintf(<span class="hljs-string">'bxb -r %s -a atr ate -L 1.txt 2.txt'</span>,record);
    system(cmd);
<span class="hljs-keyword">end</span>
system(<span class="hljs-string">'sumstats 1.txt'</span>)
system(<span class="hljs-string">'sumstats 1.txt&gt;result.txt'</span>)
</div></code></pre>
<h2 id="%E4%B8%89%E6%A3%80%E6%B5%8B%E7%BB%93%E6%9E%9C">三、检测结果</h2>
<h3 id="1%E5%9B%BA%E5%AE%9A%E9%98%88%E5%80%BC%E6%B3%95">1.固定阈值法</h3>
<h3 id="%E5%B9%B3%E5%9D%87%E6%95%8F%E6%84%9F%E5%BA%A69109%E5%B9%B3%E5%9D%87%E9%98%B3%E6%80%A7%E9%A2%84%E6%B5%8B%E7%8E%879786">（平均敏感度：91.09%；平均阳性预测率：97.86%）</h3>
<p><img src="../homework7/result(2000).jpg" alt=""></p>
<h3 id="2%E6%94%B9%E5%8F%98%E9%98%88%E5%80%BC%E6%B3%95">2.改变阈值法</h3>
<h3 id="%E5%B9%B3%E5%9D%87%E6%95%8F%E6%84%9F%E5%BA%A69859%E5%B9%B3%E5%9D%87%E9%98%B3%E6%80%A7%E9%A2%84%E6%B5%8B%E7%8E%879919">（平均敏感度：98.59%；平均阳性预测率：99.19%）</h3>
<p><img src="../homework7/result(0.2).jpg" alt=""></p>
<h3 id="3%E6%94%B9%E8%BF%9B%E6%B3%95">3.改进法</h3>
<h3 id="%E5%B9%B3%E5%9D%87%E6%95%8F%E6%84%9F%E5%BA%A69952%E5%B9%B3%E5%9D%87%E9%98%B3%E6%80%A7%E9%A2%84%E6%B5%8B%E7%8E%879926">（平均敏感度：99.52%；平均阳性预测率：99.26%）</h3>
<p><img src="../homework7/result(0.15).jpg" alt="">  <div STYLE="page-break-after: always;"></div></p>
<h1 id="%E5%BF%83%E5%BE%97%E4%BD%93%E4%BC%9A">心得体会</h1>
<p>  在本课程半学期的学习中，我们不光更加熟悉了Matlab的使用以及其代码的编写、使用C语言对信号进行处理的操作，还收获了很多新知识：从新的轻量级跨平台编译器Visual Studio Code，到将代码通过git管理起来，同时了解了markdown这种十分方便的编辑文本的语言，甚至包括远程桌面链接都是从未接触过的。</p>
<h3 id="%E8%AF%BE%E7%A8%8B">课程</h3>
<p>  在本课程中，学到了两个在编程中十分重要的概念——状态机和环形缓冲区，状态机思路在识别QRS波信号处使用，具体使用了switch函数来实现，而环形缓冲区能够减少计算时的内存，降低冗余，在很多方面都有使用，是一个很好的代替for循环数组的方式；同时掌握了部分C语言的实时算法的编写方式，其与非实时方式在输入上有很大差别，在滤波和识别处理上虽然思路异曲同工但是却在具体编写中产生了很大的分歧。</p>
<p>  第一节课主要是重温和回顾了Matlab的读取文件并画图的操作，并且绘制并了解了频谱图；</p>
<p>  第二节课依旧使用了Matlab作为编程环境，利用了filter函数让它干起了自己的老本行，对信号进行预处理——对信号进行低通滤波和高通滤波后，对处理后的信号差分来获得它的斜率信息，因为QRS波的位置一般是最为陡峭，斜率也最大；然后对一阶导信号平方，放大这种差异性并让信号为正；最后对平方信号进行积分，积分信号中的每一个方波的位置就是一次QRS信号发生的位置。</p>
<p>  有了第二次课的基础，第三、四次课我们将通过C语言编写同样的信号预处理步骤，编程平台也转移到了VS Code上，对于没有方便的filter函数的C语言，我们使用了ringbuffer的快捷方式来编写数字滤波器。</p>
<p>  第五节课上我们对预处理过后的信号做了分析和识别，因为识别QRS波发生的位置才是我们的目的。通过状态机的思想，在最初最简单的设想中，我们基于第一个人的方波信号设定了一个定值阈值2000，令寻找上升沿的状态为0，寻找下降沿的状态为1。每获取一个处理后的数据都将其与阈值比较，在0状态下若大于阈值则变更至状态1，在1状态下若小于阈值则变更至状态0并输入当前的位置数据。从而就获得了这个人的所有QRS波发生的位置。</p>
<p><img src="../pdf/流程图简单.png" alt=""><br>
  第六节课我们在第五节课已经获得了QRS波位置的基础上，计算心率并且使其能够每秒输出一次，并且也在检测到QRS波的时候输出其位置。由于可能存在这个位置即发生了QRS波，又刚好是一秒钟的时候，所以为了相互不干扰，在识别函数中对不同事件“按位或”，在主程序的逻辑条件中通过“按位与”来判断。在计算心率的时候，每一秒输出的是数组八个QRS位置一起算出来的心率的平均值。（同时为了防止数据未满时心率计算失常，稍微加了点条件）</p>
<p>  第七次课使用了自适应滤波的方法，使我们的程序能够适用于更多的数据。通过很多改进、调试、和调参后，通过wfdb库中的bxb函数计算出的敏感度和阳性预测率都能达到99%以上。具体的自适应阈值方法是对于一个人而言，获得其数据前八秒内每一秒的最大值，找到这八个最大值的中位数，乘上我们试出来最好的阈值系数0.15，将这个值作为这个人的阈值去判断。同时加入延时，防止积分信号的毛刺影响对QRS位置的判断。
<img src="../pdf/流程图真.png" alt=""></p>
<h3 id="%E8%BD%AF%E4%BB%B6">软件</h3>
<p>  由于先前未使用过Matlab完成任何整体的工程设计，所以在此课程中的学习中，同时预习和巩固了这款超强大的软件的各种功能（尤其是画图），深感其方便和强大之处。还有Markdown这个十分方便的文本格式编辑语言，很容易就能转换成html或pdf格式，还有能直接画出流程图的插件，每次作业写得很漂亮格式很工整的时候就会很开心，强迫症的福音。<br>
  VS Code真的是一个很强大的跨平台编译器，它可以支持几乎所有主流的开发语言，而且支持插件扩展，可以与Git良好兼容。在我另一门课程医学模式识别中，也需要完成一个课程设计，我使用Python语言搭建出了一个深度学习网络ResNet18来达到我的目的，而深度网络代码的编写和运行都是在VS Code平台上完成的，而没有使用略复杂的PyCharm或Jupter。</p>
<h3 id="%E4%BF%A1%E5%8F%B7%E8%AF%86%E5%88%AB">信号识别</h3>
<p>  对于信号的识别，在最初尝试的时候也许总是要从单个连续信号开始，当能准确识别第一个信号之后，再对整体的数据想办法。但是若一开始使用的是“标准人”的数据，为其设置的阈值、判断条件等可能不适用（一定不适用）于所有人，所以需要新的方法去进行判断。<br>
  一开始使用的是利用2000作为积分的阈值，对于大部分人都是很有效的，但是一个固定的阈值总会出现纰漏，这时候就需要用到自适应阈值的方式啦。我们决定使用每个人数据的前八秒获得阈值并作为他之后所有数据的判断依据。<br>
  即便如此，还是一定会出错的。在对一些识别率很低的案例进行检查的时候，发现出现了积分为负的情况——这个是因为超过了int型的界限（-32768~32767），解决方法是将积分后的数据类型改成了unsigned int，因为对差分的平方积分的方法是没有负值的。同时由于图中这种“毛刺”可能会导致误判，所以加入了延时。
<img src="../pdf/奇怪的状况0.png" alt="">
下一个问题是积分信号上出现了不同量级，因为担心这个不同寻常高的积分信号可能会影响到最后的阈值，所以没有使用均值而是使用了中位数。
<img src="../pdf/奇怪的状况1.png" alt="">
最后是发现有的积分信号甚至最大值都无法到达2000，所以最初的固定阈值从2000改到了1500。
<img src="../pdf/奇怪的状况2.png" alt="">
应该还有一些没能发现的问题，影响了一些数据的准确率。<br>
  总体而言很有意思呜呜，不光思路很重要，编程的实现方式也很重要呢，这门课程让我受益匪浅！而且帮忙捉虫的过程也是提高自己的过程嘿嘿！</p>

</body>
</html>
